//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package roadmap_content_api_serv

import (
	"atomgit.com/openlinksaas-org/api-server/config"
	"atomgit.com/openlinksaas-org/api-server/dao/roadmap_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/admin_auth_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/roadmap_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/roadmap_content_api"
)

type RoadmapContentApiImpl struct {
	roadmap_content_api.UnimplementedRoadmapContentApiServer
	adminCfg *config.AdminCfg
}

func NewRoadmapContentApiImpl(adminCfg *config.AdminCfg) *RoadmapContentApiImpl {
	return &RoadmapContentApiImpl{adminCfg: adminCfg}
}

func (apiImpl *RoadmapContentApiImpl) calcUserPerm(roadmapItem *roadmap_dao.RoadmapInfoDbItem, ownerType roadmap_api.ROADMAP_OWNER_TYPE, ownerUserId string, roadmapPerm *admin_auth_api.RoadmapPerm, isWhiteUser bool) *roadmap_api.UserPerm {
	userPerm := &roadmap_api.UserPerm{}
	if ownerType == roadmap_api.ROADMAP_OWNER_TYPE_ROADMAP_OWNER_USER {
		if ownerUserId == roadmapItem.OwnerUserId {
			userPerm.CanUpdateContent = true
			userPerm.CanRemove = true
		}
		if roadmapItem.PubState {
			if !isWhiteUser {
				userPerm.CanUpdateContent = false
			}
			userPerm.CanRemove = false
		}
	} else if ownerType == roadmap_api.ROADMAP_OWNER_TYPE_ROADMAP_OWNER_ADMIN {
		if roadmapPerm.UpdateContent && ownerUserId == roadmapItem.OwnerUserId {
			userPerm.CanUpdateContent = true
		}
		if roadmapPerm.Remove {
			userPerm.CanRemove = true
		}
		if roadmapItem.PubState {
			userPerm.CanRemove = false
		}
	}
	return userPerm
}
